<?php


class RoleAction extends SecurityAction{
	function _initialize(){
		$this->assign("menuFirst",session('menuFirst')); 
		$this->assign("menuSecond",session('menuSecond')); 
	}

		
	function rlist(){
		if ($_GET['opt'] == 'add'){  //新增跳转
			$ob = M("yq_menu");
			//查询第一级菜单
			$menulist = $ob->where(array("pid"=>"0"))->field("tid,name,pid,module,action,data,remark,ordid,display")->select();
			if(empty($menulist)){
				$this->error('没有菜单，请添加！','/Role/rlist');
				return;
			}
			$tree = $this->getMenuTree($menulist);//菜单树

			$this->assign("tree", $tree);
		    $this->display("Role/detail");
			return;
		}


	    if ($_GET["opt"] == 'edit'){   //编辑
	    	$ob = M("yq_menu");

	    	//查询第一级菜单
			$menulist = $ob->where(array("pid"=>"0"))->field("tid,name,pid,module,action,data,remark,ordid,display")->select();
			if(empty($menulist)){
				$this->error('没有菜单，请添加！','/Role/rlist');
				return;
			}
			$tree = $this->getMenuTree($menulist);//菜单树

			$ob = M("yq_role_auth");
	        $authlist = $ob->where(array("roleid"=>$_GET["tid"]))->select();  //角色、菜单

	        $ob = M("yq_role");
	        $info = $ob->where(array("tid"=>$_GET["tid"]))->find();   //角色

			$this->assign("tree", $tree);
	        $this->assign("modify", '1');
	        $this->assign("info", $info);   
	        $this->assign("authlist", json_encode($authlist)); //php数组转换json数组
	        $this->display('Role/detail');
	        return;
	    }

	    if (!empty($_GET['rolename'])){
	    	$sql[] = array('rolename' => array('like', "%{$_GET['rolename']}%"));
	    }

		$ob = M("yq_role");
		$cpp = 30; //分页数
        import('ORG.Util.Page');// 导入分页类
        $count      = $ob->where($sql)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$cpp);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        
		$list = $ob->where($sql)->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign("list", $list);
		$this->assign('show',$show);// 赋值分页输出
		$this->display("Role/rlist");
	}
	

	function detail(){
	    if($_POST){
	        //添加
	        $_POST['adminid'] = session('info.tid');
	        $_POST['add_time'] = time();//date('Y-m-d H:i:s', time());
	        $ob = M("yq_role");
	    	$roleid = '';
	        if($_POST['tid'] != ''){
	        	$roleid = $_POST['tid'];
	            $ob->where(array('tid'=>$_POST['tid']))->save($_POST);
	        }else{
	            $roleid = $ob->add($_POST);
	        }

	        //菜单不为空
	        if (!empty($_POST['menuIds'])){
	    		$ids = substr($_POST['menuIds'], 0, strlen($_POST['menuIds'])-1);
	    		$menuIds = explode(",", $ids);
	    		$auth = M('yq_role_auth');

	    		if($_POST['tid'] != ''){
	    			$auth->where(array("roleid"=>$_POST['tid']))->delete();
	    		}

	    		foreach ($menuIds as $key => $value) {
	    			$data['roleid'] = $roleid;
	    			$data['menuid'] = $value;
	    			$auth->add($data);
	    		}
	    	}

	        $this->redirect('/Role/rlist/');
	        return;
	    }
	}


	function getMenuTree($menulist){
		$treeStr = $treeStr." [";
		foreach ($menulist as $key => $value) {
			//父类
			$treeStr = $treeStr." { id:".$value['tid'].", pId:".$value['pid'].", name:'".$value['name']."', open:false},";
			
			$ob = M("yq_menu");
			$listTwo = $ob->where(array("pid"=>$value["tid"]))->select();
			if (!empty($listTwo)){
				foreach ($listTwo as $key2 => $value2) {
					//第二级
					$treeStr = $treeStr."{ id:".$value2['tid'].", pId:".$value2['pid'].", name:'".$value2['name']."'},";
				}
			}
		}
		
		return substr($treeStr,0, strlen($treeStr)-1) . "];";  
	}

}

?>